package homework.bh1505;

import javax.swing.text.html.HTMLDocument;
import java.util.*;

/*【编号：1505】现有若干图书信息（包含名称title、作者author、定价price）需要存储到set集合中，
        保证集合中无重复元素，并遍历查看。可以认为所有信息都相同的图书为重复数据。*/
public class Test2 {
    public static void main(String[] args) {
        //创建Set集合按条件排序
        Set<book> set = new TreeSet<>(new Comparator<book>() {
            @Override
            public int compare(book o1, book o2) {
                //主要条件：对价格进行升序
                int result = o1.getPrice() - o2.getPrice();
                //次要条件：对名称进行升序
                result = result == 0 ? o1.getTitle().compareTo(o2.getTitle()) : result;
                //次要条件：再对作者进行升序
             result = result == 0 ? o1.getAuthor().compareTo(o2.getAuthor()) : result;
                return result;
            }
        });
        //添加
        book b1 = new book("book3", "zhan", 45);
        book b2 = new book("booke", "lisi", 99);
        book b3 = new book("book4", "ahan", 46);
        book b4 = new book("booka", "lisi", 99);
        book b5 = new book("book3", "zhan", 45);
        //批量把书本加入集合
        Collections.addAll(set, b1, b2, b3, b4, b5);
        //增强for遍历打印
        for (book book : set) {
            System.out.println(book);
        }

    }
}

class book {
    private String title;
    private String author;
    private int price;

    public book() {
    }

    public book(String title, String author, int price) {
        this.title = title;
        this.author = author;
        this.price = price;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "名称：" + title + "    作者：" + author + "    定价：" + price + "元整";
    }
}